<!--
 * @Author: ljc
 * @Date: 2024-08-01 13:57:07
 * @LastEditors: g05047
 * @LastEditTime: 2025-01-21 10:11:12
 * @Description: 单位、区划树
-->
<template>
  <static-content-wrapper :designer="designer"
    :designHeight="true"
    :field="field"
    :design-state="designState"
    :display-style="field.options.displayStyle"
    :parent-widget="parentWidget"
    :parent-list="parentList"
    :index-of-parent-list="indexOfParentList"
    :sub-form-row-index="subFormRowIndex"
    :sub-form-col-index="subFormColIndex"
    :sub-form-row-id="subFormRowId">
    <VfAdAgTree :userInfoData="userInfo"
      :adTreeProps='filedOptions.optionConfig?.fieldNames'
      :adTreeHidden="filedOptions.adConfig?.adHidden"
      :adPlaceholder="filedOptions.adConfig?.adPlaceholder"
      :adBgc="filedOptions.adConfig?.adBgc"
      :adLabelColor="filedOptions.adConfig?.adLabelColor"
      :adTitle="filedOptions.adConfig?.adLabel"
      :adSize="filedOptions.adConfig?.adSize"
      :adLabelAlign="filedOptions.adConfig?.adLabelAlign"
      :adLabelWidth="filedOptions.adConfig?.adLabelWidth"
      :adLabelHidden="filedOptions.adConfig?.adLabelHidden"
      :adFilterable="filedOptions.adConfig?.adFilterable"
      :adRootVisible="filedOptions.adConfig?.adRootVisible"
      :adClickRepeatable="filedOptions.adConfig?.adClickRepeatable"
      :adDefaultExpandAll="filedOptions.adConfig?.adDefaultExpandAll"
      :adClickAutoExpanded="filedOptions.adConfig?.adClickAutoExpanded"
      :agTreeHidden="filedOptions.agConfig?.agHidden"
      :agPlaceholder="filedOptions.agConfig?.agPlaceholder"
      :agTitle="filedOptions.agConfig?.agLabel"
      :agSize="filedOptions.agConfig?.agSize"
      :agTreeProps='filedOptions.optionConfig?.fieldNames'
      :agBgc="filedOptions.agConfig?.agBgc"
      :agLabelColor="filedOptions.agConfig?.agLabelColor"
      :agLabelAlign="filedOptions.agConfig?.agLabelAlign"
      :agLabelWidth="filedOptions.agConfig?.agLabelWidth"
      :agLabelHidden="filedOptions.agConfig?.agLabelHidden"
      :agFilterable="filedOptions.agConfig?.agFilterable"
      :agRootVisible="filedOptions.agConfig?.agRootVisible"
      :agClickRepeatable="filedOptions.agConfig?.agClickRepeatable"
      :agDefaultExpandAll="filedOptions.agConfig?.agDefaultExpandAll"
      :agClickAutoExpanded="filedOptions.agConfig?.agClickAutoExpanded"
      @adNodeClick="handleAdNodeClick"
      @adCurrentChange="handleAdCurrentChange"
      @agNodeClick="handleAgNodeClick"
      @agCurrentChange="handleAgCurrentChange"
      @toggleHidden="handleToggleHidden">
    </VfAdAgTree>
  </static-content-wrapper>
</template>
<script >
import { VfAdAgTree } from '@/components/vf-tree';
import StaticContentWrapper from './static-content-wrapper'
import emitter from '@/utils/emitter'
import FormItemWrapper from './form-item-wrapper'
import i18n from "@/utils/i18n"
import fieldMixin from "@/components/form-designer/form-widget/field-widget/fieldMixin";
export default {
  name: "zoning-unit-tree-widget",
  componentName: 'FieldWidget',  //必须固定为FieldWidget，用于接收父级组件的broadcast事件
  mixins: [emitter, fieldMixin, i18n],
  inject: ['enumsDataPools'],
  props: {
    field: Object,
    designer: Object,
    parentList: Array,
    parentWidget: Object,
    indexOfParentList: Number,
    /** 是否为设计状态 */
    designState: { type: Boolean, default: false },
    /* 子表单组件行索引，从0开始计数 */
    subFormRowIndex: { type: Number, default: -1 },
    /* 子表单组件列索引，从0开始计数 */
    subFormColIndex: { type: Number, default: -1 },
    /* 子表单组件行Id，唯一id且不可变 */
    subFormRowId: { type: String, default: '' },
    minHeight: { type: Number },
  },
  components: {
    VfAdAgTree,
    FormItemWrapper,
    StaticContentWrapper
  },
  data() {
    return {
      loading: false,
      showCode: true,
      sOptions: [],
      expandMap: {},
      fieldModel: null,
      currentNode: {},
      oldFieldValue: null, //field组件change之前的值
      defaultExpandedKeys: [],
      //新增树字段
      userInfo: {},
      adCurreentData: {},
      agCurreentData: {},
    }
  },
  computed: {
    allowDefaultFirstOption() {
      return (!!this.field.options.filterable && !!this.field.options.allowCreate)
    },
    remoteMethod() {
      if (!!this.field.options.remote && !!this.field.options.onRemoteQuery) {
        return this.remoteQuery
      } else {
        return undefined
      }
    },
    filedOptions() {
      return this.field.options
    },
  },
  beforeCreate() {
    /* 这里不能访问方法和属性！！ */
  },
  created() {
    /* 注意：子组件mounted在父组件created之后、父组件mounted之前触发，故子组件mounted需要用到的prop
       需要在父组件created中初始化！！ */
    this.initOptionItems()
    this.initFieldModel()
    this.registerToRefList()
    this.initEventHandler()
    this.handleOnCreated()
  },
  beforeMount() {
    this.loadOptionsData()
  },
  mounted() {
    this.handleOnMounted()
  },
  beforeUnmount() {
    this.unregisterFromRefList()
  },

  methods: {
    /** 加载用户信息的方法*/
    loadOptionsData() {
      this.userInfo = this.getBusiConfig().userInfo
    },
    /** 区划树方法点击方法*/
    handleAdNodeClick(data, treeNode) {
      this.handleOnAdAgChange(data, 'onAdNodeClick')
    },
    /** 区划树方法切换方法*/
    handleAdCurrentChange(data, treeNode) {
      this.adCurreentData = {
        ...data
      }
      this.handleOnAdAgChange(data, 'onAdCurrentChange')
    },
    /** 单位树点击方法*/
    handleAgNodeClick(data, treeNode) {
      this.handleOnAdAgChange(data, 'onAgNodeClick')
    },
    /** 单位树切换方法*/
    handleAgCurrentChange(data, treeNode) {
      this.agCurreentData = {
        ...data
      }
      this.handleOnAdAgChange(data, 'onAgCurrentChange')
    },
    /**区划单位树是否隐藏 */
    handleToggleHidden(hidden) {
      this.handleOnToggleHidden(hidden, 'onToggleHidden')
    },
    /**获取当前区划单位树的数据*/
    getCurrentTreeData() {
      return {
        adCurreentData: this.adCurreentData, // 区划
        agCurreentData: this.agCurreentData, // 单位
      }
    }
  }
}

</script>
<style lang="scss" scoped>
/* form-item-wrapper已引入，还需要重复引入吗？ */
@import "@/styles/global.scss";
.full-width-input {
  width: 100% !important;
}
</style>

